package cate.game.play.skill.passive.hero;

import cate.common.table.d.GDBuff;
import cate.common.util.XT;
import cate.game.play.buff.BuffParam;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.result.EnergyChangeResult;
import cate.game.play.skill.passive.PassiveHandler;
import lombok.Getter;

import java.util.List;

/**
 * 英雄觉醒
 */
public class 英雄觉醒PH extends PassiveHandler {

	private double 初始值;
	private double 回合增加;
	private List<Integer> 觉醒buff;

	//初始值=75&回合增加=25觉醒buff=？？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		初始值 = args.getDouble("初始值", 0d);
		回合增加 = args.getDouble("回合增加", 0d);
		觉醒buff = args.getIntList("觉醒buff");
	}

	private static final int PSY_ENG_MAX = 100;
	private static final int DEFAULT_ROUND = 3;

	//当前觉醒值
	@Getter
	private int curValue;

	@Override
	public void parseExt(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		初始值 += args.getDouble("初始值", 0d);
		回合增加 += args.getDouble("回合增加", 0d);
	}

	public int getAwakenBuff() {
		if (XT.isNotBlank(觉醒buff)) {
			return 觉醒buff.get(0);
		}
		return -1;
	}

	public void addEnergy(ActionCtx action, double value) {
		addEnergy(action, false, value);
	}


	private void addEnergy(ActionCtx action, boolean auto, double value) {
		if (value > 0 && skill.owner.buff.containType(GDBuff.Type.觉醒失效)) {
			return;
		}
		this.curValue += value;
		if (value > 0) {
			this.curValue = Math.min(curValue, PSY_ENG_MAX);
		} else {
			this.curValue = Math.max(curValue, 0);
		}
		EnergyChangeResult result = new EnergyChangeResult().pidSet(skill.owner.getPid()).valSet(value).curSet(curValue);
		action.addResult(result);
		skill.owner.skill.forHandlers(h -> h.onEnergyChange(action,auto, value));
	}

	@Override
	public void onFirstEnter(ActionCtx action) {
		addEnergy(action,true,初始值);
	}

	@Override
	public void onRoundEnd(ActionCtx action) {
		if (skill.owner.isDead()) {
			return;
		}
		addEnergy(action,true, 回合增加);
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		if (skill.owner.isDead()) {
			return;
		}
		if (curValue < PSY_ENG_MAX) {
			return;
		}
		addEnergy(action,true, -curValue);
		doAwaken(action);
	}

	private void doAwaken(ActionCtx action) {
		doAwaken(action, DEFAULT_ROUND);
	}

	public void doAwaken(ActionCtx action, int assignRound) {
		if (skill.owner.buff.containType(GDBuff.Type.觉醒失效)) {
			return;
		}
		if (assignRound <= 0) {
			return;
		}
		BuffParam param = new BuffParam().exRoundSet(assignRound - DEFAULT_ROUND);
		for (Integer tid : 觉醒buff) {
			skill.owner.buff.tryAddByTid(action, skill.owner, tid, param);
		}
	}
}
